﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HotelRegistrationSystem.Models;

namespace HotelRegistrationSystem.DAL
{
    public class RoomDAL
    {
        public static Dictionary<int, RoomTypeModel> GetRoomTypesDictionary()
        {
            using (HotelRegistrationSystemEntities db = new HotelRegistrationSystemEntities())
            {
                var types =
                    (from t in db.RoomTypes
                     select new RoomTypeModel
                     {
                         RoomTypeId = t.RoomTypeId,
                         Type = t.Type,
                         ReserveWhole = t.ReserveWhole,

                     }).ToDictionary(rt => rt.RoomTypeId, rt => rt);

                return types;
            }
        }

        public static List<OneRoomReservationModel> GetRoomByCriteria(int id, int type, bool typeSpecified)
        {
            var types = GetRoomTypesDictionary();

            using (HotelRegistrationSystemEntities db = new HotelRegistrationSystemEntities())
            {
                var result =
                    (from r in db.Rooms
                     join rt in db.RoomTypes on r.RoomTypeId equals rt.RoomTypeId
                     where (typeSpecified && r.HotelId == id && r.RoomType.RoomTypeId == type) ||
                     (!typeSpecified && r.HotelId == id)
                     select new OneRoomReservationModel
                     {
                        RoomId = r.RoomId,
                        RoomName = r.Name,
                        BedCount = r.BedCount,
                        Price = r.Price,
                        RoomTypeId = r.RoomTypeId
                     }).ToList();

                foreach (OneRoomReservationModel room in result)
                {
                    var innerresult =
                        (from rr in db.ReservationsRooms
                         where rr.RoomId == room.RoomId
                         select new OneReservationModel
                         {
                             StartDate = rr.Reservation.StartDate,
                             Period = rr.Reservation.Period,
                             ReservationStatusId = rr.Reservation.ReservationStatus.ReservationStatusId,
                             BedCount = rr.Reservation.ReservationsRooms.Where(x => x.RoomId == room.RoomId).Sum(x => (int)x.Count)
                         }).ToList();
                    room.Reservations = innerresult;
                    room.RoomType = types[room.RoomTypeId];
                }

                return result.ToList();
            }
        }

        public static List<OneRoomReservationModel> GetAllRoomsByHotel(int id)
        {
            return GetRoomByCriteria(id, -1, false);
        }

        public static List<OneRoomReservationModel> GetRoomByHotelRoomType(int id, int type)
        {
            return GetRoomByCriteria(id, type, true);
        }

        public static string GetRoomName(int reservationId)
        {
            using (var context = new HotelRegistrationSystemEntities())
            {
                return context.ReservationsRooms.FirstOrDefault(r => r.ReservationId == reservationId).Room.Name;
            }
        }
        public static List<RoomType> GetRoomTypes()
        {
            using (HotelRegistrationSystemEntities db = new HotelRegistrationSystemEntities())
            {
                return db.RoomTypes.ToList();
            }
        }
    }
}
